/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-10-20 17:40:38
 * @LastEditors: cgt
 * @LastEditTime: 2022-01-13 11:03:26
 */
/**
 * @module init/initImages
 * @desc 初始化地图图标
 */

import { loadImage, loadSpriteImage } from '../utils/ImageUtil';
import ImageList from '../../../constant/ImageList';

/**
 * 初始化地图图标
 * @param {Map} top 地图开放顶层
 * @param {*} impl 地图实现
 */
export function initImages(top, impl) {
  let imageList = [].concat(ImageList.images);
  let customImageList = top.config.get('IMAGE_LIST');
  if (customImageList && customImageList.length > 0) {
    imageList = customImageList.concat(imageList);
  }
  impl.map.on('styleimagemissing', function(event) {
    let image = imageList.find((item) => {
      return item.id === event.id;
    });
    if (image) {
      // console.log("加载图片" + image.id, top.config.get("RESOURCE_BASEURL") + image.url);
      loadImage(impl.map, image.id, top.config.get('RESOURCE_BASEURL') + image.url);
    } else if(event.id !== 99 && event.id !== 0) {
      console.log('未在ImageList中找到图片，图片id为:' + event.id);
      // 从默认位置加载
      loadImage(impl.map, event.id, top.config.get('RESOURCE_BASEURL') + 'missing/' + event.id + '.png');
    }
  });

  // 加载雪碧图
  let spriteList = [].concat(ImageList.sprites);
  let customSpriteList = top.config.get('SPRITE_IMAGE');
  if (customSpriteList && customSpriteList.length > 0) {
    spriteList = customSpriteList.concat(spriteList);
  }
  spriteList.forEach((spriteConfig) => {
    let image = top.config.get('RESOURCE_BASEURL') + spriteConfig.image;
    let json = top.config.get('RESOURCE_BASEURL') + spriteConfig.index;
    loadSpriteImage(impl, image, json);
  });
}
